<?php
declare (strict_types = 1);
namespace app\model;
use think\Model;
/**
 * @mixin \think\Model
 */
class UserUser extends Model
{   /*
      注册账号
     return json
    */
    public function userCreate($data){
        $data['status']=0;
        try {
            //创建账号  mysql
            $this->save($data);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return  json([
                'code'=>400,
                'msg'=>$e->getMessage()
            ]);
        }
           return json([
            'code' => 200,
            'msg' => 'success'
        ]);
    }
    /*
      登陆账号
     return json
    */
    public function userLogin($data){
        try{
            //查询用户输入账号的数据 mysql
            $email=$this->where('email',$data['email'])->find();
            if(!isset($email)){
                throw new \Exception("该邮箱未注册");
            }else{
                if($data['password']!== $email->password){
                    throw new \Exception("密码错误");
                }
                $updateData= [
                    'last_landing' => time(),
                    'token' => $data['token'],
                    'token_out' => $data['token_out']
                ];
                 //插入登陆时间 token  mysql
                $this->where('id',$email->id)->update($updateData);
                $msgData= [
                    'username' => $email->username,
                    'image' => $email->image,
                    'last_landing' => $updateData['last_landing'],
                    'token' => $data['token'],
                    'token_out' => $data['token_out'],
                ];
                // session('userData',$msgData);
                session_id() || @session_start();
                $_SESSION['userData'] = $msgData;
                return json([
                'code' => 200,
                'msg' => $msgData,
                ]);
            }   
        }catch(\Exception $e){
            return  json([
                'code' => 400,
                'msg' => $e->getMessage()
            ]);
        }

    }
    /*
      个人中心
     array    $data
     Boolean  $image 是否含头像
     return json
     
    */
    public function avatar($data,$image){
        if($image){
                $this->where('token',$data['token'])->update(['username'=>$data['username'],'image'=>$data['image']]);
                session_id() || @session_start();
                $_SESSION['userData']['username'] = $data['username'];
                $_SESSION['userData']['image'] = $data['image'];
                return json([
                    'code' => 200,
                    'msg' => "success",
                    'image'=>$data['image']
                ]);
        }else{
             $this->where('token',$data['token'])->update(['username'=>$data['username']]);
                session_id() || @session_start();
                $_SESSION['userData']['username'] = $data['username'];
                return json([
                    'code' => 200,
                    'msg' => "success",
                ]);
        }
       
   
    }
}
